var express = require('express');
var router = express.Router();
var rest = require('rest');
module.exports = router;
var apiPath = 'http://globr-api.herokuapp.com/api/search';
//var apiPath = 'http://localhost:8080/globr/api/search';
router.get('/search.html', function (req, res) {
    var keyword = req.param('keyword');
    var pagNum = req.param('pagNum');
    if (!keyword) {
        res.redirect("/");
    }
    if (!pagNum) {
        pagNum = 1;
    }

    var list;
    var sourceUrl = 'https://www.google.com/#q=' + keyword + '&safe=&hl=en&off&start=' + (pagNum - 1) * 10;
    //这里执行搜索Api的调用
    rest(apiPath + '?keyword=' + encodeURI(keyword) + '&pagNum=' + pagNum).then(function (resp) {
        list = JSON.parse(resp.entity).list;
        var resultInfo = {
            keyword: keyword,
            index: (parseInt(pagNum) + 1),
            list: list,
            sourceUrl: sourceUrl
        }
        console.info(resultInfo.list);
        res.render('search', resultInfo);
    });
});